import {
    request
} from "../../request/index.js";
var redis = require('../../utils/redis.js').default;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orders: [],
        tabs: [{
                id: 0,
                value: "全部",
                isActive: true
            },
            {
                id: 1,
                value: "待付款",
                isActive: false
            },
            {
                id: 2,
                value: "待发货",
                isActive: false
            },
            {
                id: 3,
                value: "待确认",
                isActive: false
            },
            {
                id: 4,
                value: "待评价",
                isActive: false
            },
            {
                id: 5,
                value: "退款/售后",
                isActive: false
            }
        ]
    },

    onShow(options) {
        const token = redis.get('token')
        if (!token) {
            wx.navigateTo({
                url: '/page/login/index'
            });
            return;
        }

        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1];
        const {
            type
        } = currentPage.options;
        this.changeTitleByIndex(Number(type) + 1);
        this.getOrders(type);
    },
    // 获取订单列表的方法
    async getOrders(type) {
        if (type === 4) {
            type = 5;
        }
        // const userId = wx.getStorageSync("userId");
        const userId = redis.get("userId");
        const res = await request({
            url: "/orders/query",
            data: {
                orderState: type,
                userId
            }
        });
        this.setData({
            orders: res.data.data.records
        })
    },
    changeTitleByIndex(index) {
        let {
            tabs
        } = this.data;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
        this.setData({
            tabs
        })
    },
    handleTabsItemChange(e) {
        const {
            index
        } = e.detail;
        this.changeTitleByIndex(index);
        this.getOrders(index - 1);
    },
    // 付款
    handlePay(e) {
        wx.navigateTo({
            url: '/pages/order/pay/index?orderSn=' + e.currentTarget.dataset.ordersn,
        })
    },
    handleCancel(e) {
        let _this = this;
        wx.showModal({
            title: '提示',
            content: '确认取消订单？',
            async success(res) {
                if (res.confirm) {
                    let orderSn = e.currentTarget.dataset.ordersn;
                    const res = await request({
                        url: "/orders/cancelOrder",
                        method: "POST",
                        data: orderSn,
                    });
                    if (res.data.code === 200) {
                        wx.showToast({
                            title: '取消成功',
                            icon: 'success'
                        })
                        setTimeout(function () {
                            _this.onShow();
                        }, 500)
                    } else {
                        wx.showToast({
                            title: '取消失败',
                            icon: 'error'
                        })
                    }
                }
            }
        })
    },
    handleConfirm(e) {
        let _this = this;
        wx.showModal({
            title: '提示',
            content: '是否确认收货？',
            async success(res) {
                if (res.confirm) {
                    let orderSn = e.currentTarget.dataset.ordersn;
                    const res = await request({
                        url: "/orders/confirmOrder",
                        method: "POST",
                        data: orderSn,
                    });
                    if (res.data.code === 200) {
                        wx.showToast({
                            title: '操作成功',
                            icon: 'success'
                        })
                        setTimeout(function () {
                            _this.onShow();
                        }, 500)
                    } else {
                        wx.showToast({
                            title: '操作失败',
                            icon: 'error'
                        })
                    }
                }
            }
        })
    }

})